package com.motion.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.motion.dto.RecordHealth;
import com.motion.dto.UserHealthDto;
import com.motion.dto.UserTrainDto;
import com.motion.generator.entity.Feedback;
import com.motion.generator.entity.User;
import com.motion.generator.entity.UserData;
import com.motion.generator.mapper.UserDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
@Service
public class UserDataService {
    @Autowired(required = false)
    private UserDataMapper userDataMapper;
    public boolean isExistUserData(long userID){
        UpdateWrapper<UserData> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("userID",userID);
        List<UserData> lst = userDataMapper.selectList(updateWrapper);
        return lst.size()>0;

    }

    public void creatUserData(UserData userData){
        userDataMapper.insert(userData);
    }

    public void updateUserData(UserData userData){
        userDataMapper.update(userData, new UpdateWrapper<UserData>().eq("userID",userData.getUserId()));
    }

    public UserHealthDto userHealthDto(long id){

        UserHealthDto userHealthDto=null;
        UserData userData=userDataMapper.selectById(id);
        QueryWrapper<UserData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("UserID",id);

        double height = userDataMapper.selectOne(queryWrapper).getHeight();
        userHealthDto.setHeight(height);

        double weight = userDataMapper.selectOne(queryWrapper).getWeight();
        userHealthDto.setWeight(weight);
        return userHealthDto;
    }

    public boolean insertHealthData(UserData userData){
        userDataMapper.insert(userData);
        if(userData.getUserId()<0){
            return false;
        }else {
            return true;
        }
    }

    public IPage<UserData> getUserDataList(Long useId,Long page,Long size){
        QueryWrapper<UserData> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("userId",useId);
        queryWrapper.orderByDesc("recordTime");
        if(userDataMapper.selectCount(queryWrapper)!=0){
            return userDataMapper.selectPage(new Page<>(page,size),queryWrapper);
        }else {
            return new Page<>(page,size);
        }

    }

    public List<RecordHealth> userData2RecordHealth(List<UserData> userDataList){
        List<RecordHealth> recordHealthList=new ArrayList<RecordHealth>();
        RecordHealth tem =new RecordHealth();
        for (UserData item:userDataList){
            tem=new RecordHealth();
            tem.setHeight(item.getHeight());
            tem.setWeight(item.getWeight());
            tem.setRecordId(item.getUserDataId());
            tem.setBmi(item.getWeight()/Math.pow(item.getHeight().floatValue()*0.01,2));
            tem.setCreateTime(item.getRecordTime());
            if(tem.getBmi()<18.5){
                tem.setStatus(1);
            }else if(tem.getBmi()<24){
                tem.setStatus(2);
            }else if(tem.getBmi()<28){
                tem.setStatus(3);
            }else {
                tem.setStatus(4);
            }
            recordHealthList.add(tem);
        }
        return recordHealthList;
    }

    public UserData getUserDataByUserId(Long userId){
        QueryWrapper<UserData> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("userId",userId);
        return userDataMapper.selectOne(queryWrapper);
    }

//    public UserTrainDto userTrainDto(long id){
////        UserTrainDto userTrainDto = null;
////        UserData userData=userDataMapper.selectById(id);
////        QueryWrapper<UserData> queryWrapper = new QueryWrapper<>();
////        queryWrapper.eq("UserID",id);
////
////        int maxDay = userDataMapper.selectOne(queryWrapper).getMaxDay();
////        userTrainDto.setMaxDay(maxDay);
////        long courseNum = userDataMapper.selectOne(queryWrapper).getCoursesNumber();
////        userTrainDto.setCourseNum(courseNum);
////
////        String videoDuration = userTrainDto.getVideoDuration();
////        userTrainDto.setVideoDuration(videoDuration);
////
////        LocalDateTime creatTime = userTrainDto.getCreatTime();
////        userTrainDto.setCreatTime(creatTime);
//        return userTrainDto;
//    }
}
